package com.pai4j.aigc.config;


import com.pai4j.aigc.consumer.redis.AiBotChatRedisQueueListener;
import com.pai4j.aigc.consumer.redis.RobotChatRedisQueueListener;
import com.pai4j.common.enums.messagequeue.MessageBroadChannelEnum;
import jakarta.annotation.Resource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.listener.ChannelTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
public class RedisConfig {

    @Resource
    private RobotChatRedisQueueListener robotChatRedisQueueListener;
    @Resource
    private AiBotChatRedisQueueListener aiBotChatRedisQueueListener;
    @Bean
    public RedisTemplate<String, String> stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<String, String> template = new RedisTemplate<>();
        template.setConnectionFactory(redisConnectionFactory);
        
        // 设置字符串序列化器
        StringRedisSerializer stringSerializer = new StringRedisSerializer();
        template.setKeySerializer(stringSerializer);
        template.setValueSerializer(stringSerializer);
        template.setHashKeySerializer(stringSerializer);
        template.setHashValueSerializer(stringSerializer);
        
        template.afterPropertiesSet();
        return template;
    }

    @Bean
    public RedisMessageListenerContainer container(RedisConnectionFactory redisConnectionFactory) {
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        // 设置链接工厂
        container.setConnectionFactory(redisConnectionFactory);
        // 配置消息监听器
        container.addMessageListener(robotChatRedisQueueListener, new ChannelTopic(MessageBroadChannelEnum.QUEUE_CHAT_AI_BOT.getChannel()));
        container.addMessageListener(aiBotChatRedisQueueListener, new ChannelTopic(MessageBroadChannelEnum.QUEUE_CHAT_ROBOT.getChannel()));
        return container;
    }
}
